<template>
  <div class="app-container">
    <AddBtn :handleClick="handleCreate" :text="text"/>

    <el-table :data="textModelList" border style="width: 100%" v-loading="listLoading">
      <el-table-column align="center" label="ID" prop="id" width="95" type="index"></el-table-column>
      <el-table-column prop="name" label="名称" width="250"></el-table-column>
      <el-table-column prop="isPublic" label="类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isPublic=== true" type="primary">公有</el-tag>
          <el-tag v-else type="warning">私有</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="300" align="center">
        <template slot-scope="scope">
          <el-button size="small" type="warning" @click="handleUpdate(scope.$index, scope.row)">
            <i class="el-icon-edit"></i>
          </el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDel(scope.$index, scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form label-width="120px" :model="Form" :rules="FormRules" ref="dataForm">
        <el-form-item label="模板名称" prop="name" width="100%">
          <el-input v-model="Form.name"></el-input>
        </el-form-item>
        <el-form-item label="模板类型" prop="isPublic">
          <el-select v-model="Form.isPublic" class="filter-item" placeholder="请选择类型">
            <el-option v-for="item in TypeOptions" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="模板内容" prop="content" width="100%" >
          <el-input v-model="Form.content" type="textarea" :rows="2"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="dialogConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import AddBtn from "@/views/layout/components/AddBtn";
import {
  getTextModelList,
  getTextModelData,
  createTextModel,
  updateTextModel,
  deleteTextModel
} from "@/api/textModel";
import { mapGetters } from "vuex";
export default {
  props: {
    deviceType: {
      type: Number,
      default: 1
    }
  },
  components: { AddBtn },
  data(){
    return {
      text: "文字模板",
      textModelList:[],
      listLoading:false,
      dialogStatus: "",
      textMap: {
        createIncubator: "新增保温箱文字模板",
        createCar: "新增冷藏车文字模板",
        createWarehouse: "新增库房文字模板",
        updateIncubator: "编辑保温箱文字模板",
        updateCar: "编辑冷藏车文字模板",
        updateWarehouse: "编辑库房文字模板", 
      },
      TypeOptions: [{ value: true, label: "公有" }, { value: false, label: "私有" }],
      dialogFormVisible: false,
      Form: {
        name: "",
        isPublic: null,
        content:''
      },
      FormRules: {
        name: [{ required: true, message: "请输入文字模板名称", trigger: "blur" }],
        isPublic: [{ required: true, message: "请选择类型", trigger: "change" }],
        content: [{ required: true, message: "请输入文字模板内容", trigger: "blur" }],
      }

    }
  },
   computed: {
    ...mapGetters(["customerid"])
  },


  methods:{
    handleCreate() {

    },

    //获取列表
    getList() {
      this.listLoading = true;
      getTextModelList({
        customerId: this.customerid,
        deviceType: this.deviceType
      }).then(response => {
          this.textModelList = response.data;
          this.listLoading = false;
        })
        .catch(e => {});
    },

    // 弹出框确定按钮
    dialogConfirm() {
      if ( (this.dialogStatus == "createIncubator") || (this.dialogStatus == "createCar") || (this.dialogStatus == "createWarehouse") ) {
        this.textModelCreate();
      } else {
        this.textModelUpdate();
      }
    },
    // 新增模板弹窗
    handleCreate() {
      this.Form = {
        name: "",
        isPublic: null,
        content:''
      };
      if (this.deviceType === 1) {
        this.dialogStatus = "createIncubator";
      } else if (this.deviceType === 2) {
        this.dialogStatus = "createCar";
      } else {
        this.dialogStatus = "createWarehouse";
      }
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
    },

    //新增文字模板
    textModelCreate() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消"
          }).then(() => {
            let tempData = Object.assign({}, this.Form, { customerId: this.customerid }, { deviceType: this.deviceType });
            createTextModel(tempData).then(res => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: "成功",
                message: "新增文字模板成功",
                type: "success",
                duration: 2000
              });
            }).catch(e=>{});
          });
        }
      });
    },

    //显示编辑界面
    handleUpdate(index, row) {
      this.Form = Object.assign(this.Form, row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.clearValidate();
      });
    },

    //编辑文字模板
    textModelUpdate() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.Form);
          updateTextModel(tempData).then(() => {
            this.getList();
            this.dialogFormVisible = false;
            this.$notify({
              title: "成功",
              message: "更新文字模板成功",
              type: "success",
              duration: 2000
            });
          });
        }
      });
    },

    //删除文字模板
    handleDel(index, row) {
      this.$confirm("确认删除该文字模板吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.listLoading = true;
        deleteTextModel({ textModelId: row.id }).then(() => {
          this.listLoading = false;
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.getList();
        });
      }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },

  },
  mounted(){
    this.getList();
  }
}
</script>

